package com.atmilan.quartz.utils.product;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.atmilan.quartz.config.RestTemplateConfig;
import com.atmilan.quartz.model.dto.coltorti.ColtortiProductResDTO;
import com.atmilan.quartz.model.dto.hiqer.HiQErProductResDTO;
import com.atmilan.quartz.model.dto.im.ImProductResDTO;
import com.atmilan.quartz.model.entity.crmeb.SystemConfig;
import com.atmilan.quartz.utils.unibuy.util.MD5Util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * 拉取商品源数据
 */

@Slf4j
@Service
public class HiQErProductUtil {

    @Autowired
    private RestTemplateConfig restTemplateConfig;
    @Value("${hiqer.appkey}")
    private String hiqerAppKey;
    @Value("${hiqer.partnerId}")
    private String hiqerPartnerId;
    @Value("${hiqer.url}")
    private String hiqeriUrl;
    //一般贸易
    @Value("${hiqer.hiqerGoodsUrl}")
    private String hiqerGoodsUrl;
    //跨境
    @Value("${hiqer.hiqerKJGoodsUrl}")
    private String hiqerKJGoodsUrl;

    public HiQErProductResDTO selectHiQerPage(Integer pageNow, Integer pageSize, Integer type) throws Exception {
        HiQErProductResDTO hiQErProductResDTO = null;
        try {
            RestTemplate restTemplate = restTemplateConfig.restTemplate();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            String sign = "";
            String url = "";
            JSONObject param = new JSONObject();

            param.put("partnerid", hiqerPartnerId);
            param.put("page", pageNow);
            param.put("pagesize", pageSize);

            sign = getSign(param);
            headers.add("sign", sign);

            if (type == 1) {
                url = this.hiqeriUrl + this.hiqerGoodsUrl;
            } else {
                url = this.hiqeriUrl + this.hiqerKJGoodsUrl;
            }
            HttpEntity<JSONObject> formEntity = new HttpEntity<>(param, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url, formEntity, String.class);
            ObjectMapper mapper = new ObjectMapper();
            hiQErProductResDTO = mapper.readValue(response.getBody(), HiQErProductResDTO.class);
            if (HttpStatus.OK != response.getStatusCode()) {
                JSONObject message = JSONObject.parseObject(response.getBody());
//			    String errorMessage = message.getString("error");

                log.info("---------------haiqer分页商品数据异常: {} ---------------", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return hiQErProductResDTO;
    }

    /**
     * 参数签名
     *
     * @author daigb
     * @date: 2023-06-14
     */
    public String getSign(Map<String, Object> signMap) {
        StringBuilder signString = new StringBuilder();
        try {
            signString.append(hiqerAppKey);

            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(signMap);
            signString.append(json);
            signString.append(hiqerAppKey);

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return MD5Util.encrypt(signString.toString()).toUpperCase();
    }
}
